#pragma once
#include <windows.h>
#include <d3dx9.h>

#include <vector>
#include <string>
using std::vector;
using std::string;

#include "Common.h"
#include "String.h"
#include "texture.h"
#include "font.h"
#include "../frame/frame.h"
#include "../renderer/Sprite.h"


const D3DXVECTOR2 default_ui_pos        = D3DXVECTOR2( 0, 0 );
const UINT default_ui_width                     = 100;
const UINT default_ui_height            = 100;

const string button_texture                     = "";
const string selector_texture           = "";
const String text_font                          = TEXT ( "" );

const UINT top                                          = 0;


class TgoUIBase;
class TgoUIButton;
class TgoUIStatic;

class UIEntity
{
public:
        friend class TgoUISprite;

							UIEntity( TgoUIBase* ui );
							~UIEntity();
	void					SetupUIEntity( TgoUIBase* ui );
	void					UpdateEntity( float dt );
	void					ReleaseEntity();

private:
        TgoUIBase*                              mUIBase;
        Tgo2DEntity*                    mEntity;
        vector< UIEntity* >             mChildren;
};

class TgoUISprite
{
public:

							TgoUISprite();
							~TgoUISprite();

        void                                    InitUISprite();
        void                                    UpdateUISprite( float dt );
        void                                    ReleaseUISprite();
        void                                    Render();
        void                                    RenderUI( UIEntity* ui );

        void                                    AddUIEntity( UIEntity* ui );
private:
        ID3DXSprite*                    mUISprite;
        vector< UIEntity* >             mRootUIEntity;
};

class TgoUI
{
public:
                                                        TgoUI();
                                                        ~TgoUI();

        void                                    OnInitUI();
        void                                    OnUpdateUI( float dt );
        void                                    OnReleaseUI();

        void                                    AddUI( TgoUIBase* ui );
        void                                    RenderUI();

        //events
        void                                    LButtonDown();
        void                                    LButtonUp();
        void                                    RButtonDown();
        void                                    RButtonUp();
        void                                    Click();
        void                                    MouseHover();

private:
        void                                    SetupUI( TgoUIBase* ui );

        vector< TgoUIBase* >    mRootUI;
        TgoUISprite                             mUISprite;
};


class TgoUIBase
{
public:
                                                        TgoUIBase( D3DXVECTOR2 pos = default_ui_pos, 
                                                                           UINT width = default_ui_width, 
                                                                           UINT height = default_ui_height,
                                                                           bool isRoot = false );
        virtual                                 ~TgoUIBase();

        virtual void                    OnUpdate( float dt );
        void                                    AddChild( TgoUIBase* child );

		void									SetTexture( string texKey ) { mTexKey = texKey; }
        void                                    SetText( String text ) { mText = text; }
        void                                    SetParent( TgoUIBase* parent ) { mParent = parent; }
        void                                    SetPos( D3DXVECTOR2 pos ) { mPos = pos; }
        void                                    SetWidth( UINT width ) { mWidth = width; }
        void                                    SetHeight( UINT height ) { mHeight = height; }

        void                                    Show() { mIsVisible = true; }
        void                                    Hide() { mIsVisible = false; }

	void					Active() { mIsActive = true; }
	void					DeActive() { mIsActive = false; }

	bool					IsActive() { return mIsActive; }
	bool					IsVisible() { return mIsVisible; }
	bool					IsClicked();
	bool					IsMouseHover();
	bool					IsLButtonDown();
	bool					IsRButtonDown();

        UINT                                    GetWidth() { return mWidth; }
        UINT                                    GetHeight() { return mHeight; }
        String                                  GetText() { return mText; }
        string                                  GetTexture() { return mTexKey; }
        TgoUIBase*                              GetParent() { return mParent; }
        vector< TgoUIBase* >    GetChildren() { return mChildren; }
        D3DXVECTOR2                             GetPos() { return mPos; }

	virtual void			MouseHover();
	virtual void			LButtonUp();
	virtual void			LButtonDown();
	virtual void			RButtonUp();
	virtual void			RButtonDown();
	virtual void			Click() { }

        virtual bool                    InUI();
protected:
        D3DXVECTOR2                             mPos;
        UINT                                    mWidth;
        UINT                                    mHeight;
        String                                  mText;
        string                                  mTexKey;

        bool                                    mIsRoot;
        bool                                    mIsActive;
        bool                                    mIsVisible;
        bool                                    mIsClicked;
        bool                                    mIsMouseHover;
        bool                                    mIsLBtnDown;
        bool                                    mIsRBtnDown;

        vector< TgoUIBase* >    mChildren;
        TgoUIBase*                              mParent;
};

class TgoUIButton:public TgoUIBase
{
public:
        class TgoUISelector:public TgoUIBase
        {
        public:
                                        TgoUISelector( D3DXVECTOR2 pos = default_ui_pos,
                                                                   UINT width = default_ui_width,
                                                                   UINT height = default_ui_height,
                                                                   bool isRoot = false );

        };
public:
                                                        TgoUIButton( D3DXVECTOR2 pos = default_ui_pos, 
                                                                                  UINT width = default_ui_width, 
                                                                                  UINT height = default_ui_height,
                                                                                  bool isRoot = false );
                                                        ~TgoUIButton();

	void					MouseHover();
	void					LButtonDown();
	void					LButtonUp();
	void					SetTexture( string texture ) { mTexKey = texture; }
	void					SetSelectTexture( string texture ) { mSelTexture = texture; }
	void					SetUnSelectTexture( string texture ) { mUnSelTexture = texture; }

        bool                                    IsBtnDown() { return mBtnDown; }
private:
        bool                                    mBtnDown;
		string									mSelTexture;
		string									mUnSelTexture;
};

class TgoUIStatic:public TgoUIBase
{
public:
							TgoUIStatic( D3DXVECTOR2 pos = default_ui_pos,
										 UINT width = default_ui_width,
										 UINT height = default_ui_height,
										 bool isRoot = false ):TgoUIBase( pos, width, height, isRoot ) {  }//Hide(); }
};

class TgoUIListBox:public TgoUIBase
{
public:
							TgoUIListBox( D3DXVECTOR2 pos = default_ui_pos,
										  UINT width = default_ui_width,
										  UINT height = default_ui_height,
										  bool isRoot = false );

	void					SetSelectorTexture( string tex );
	void					HideSelector(){ GetChildren()[ 0 ]->Hide(); }
	UINT					GetFocusItem();


	void					AddItem( String item, string tex );
	void					MouseHover();
	void					LButtonDown();
	void					LButtonUp();
private:
	UINT					mItemHeight;
	UINT					mFocusItem;
	UINT					mRetItem;
};


namespace TGO
{
        TgoUI&  GetTgoUI();

	bool UIWndProc( UINT message, WPARAM wParam, LPARAM lParam );
}